package org.jeecg.modules.oms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.oms.entity.SemiProductSalesOrderDetail;
import org.jeecg.modules.oms.service.ISemiProductSalesOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 半成品销售订单明细Controller
 */
@Api(tags = "半成品销售订单明细")
@RestController
@RequestMapping("/oms/semiProductSalesOrderDetail")
@Slf4j
public class SemiProductSalesOrderDetailController extends JeecgController<SemiProductSalesOrderDetail, ISemiProductSalesOrderDetailService> {

    @Autowired
    private ISemiProductSalesOrderDetailService semiProductSalesOrderDetailService;

    /**
     * 分页列表查询
     */
    @AutoLog(value = "半成品销售订单明细-分页列表查询")
    @ApiOperation(value = "半成品销售订单明细-分页列表查询", notes = "半成品销售订单明细-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SemiProductSalesOrderDetail>> queryPageList(SemiProductSalesOrderDetail semiProductSalesOrderDetail,
                                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                   HttpServletRequest req) {
        try {
            Page<SemiProductSalesOrderDetail> page = new Page<>(pageNo, pageSize);
            IPage<SemiProductSalesOrderDetail> pageList = semiProductSalesOrderDetailService.queryDetailPage(page, semiProductSalesOrderDetail);
            return Result.OK(pageList);
        } catch (Exception e) {
            log.error("查询订单明细列表失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据订单ID查询明细列表
     */
    @AutoLog(value = "半成品销售订单明细-根据订单ID查询")
    @ApiOperation(value = "半成品销售订单明细-根据订单ID查询", notes = "半成品销售订单明细-根据订单ID查询")
    @GetMapping(value = "/getByOrderId")
    public Result<List<SemiProductSalesOrderDetail>> getByOrderId(@RequestParam(name = "orderId", required = true) String orderId) {
        try {
            List<SemiProductSalesOrderDetail> list = semiProductSalesOrderDetailService.getByOrderId(orderId);
            return Result.OK(list);
        } catch (Exception e) {
            log.error("根据订单ID查询明细失败: orderId={}", orderId, e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 添加
     */
    @AutoLog(value = "半成品销售订单明细-添加")
    @ApiOperation(value = "半成品销售订单明细-添加", notes = "半成品销售订单明细-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SemiProductSalesOrderDetail semiProductSalesOrderDetail) {
        try {
            boolean result = semiProductSalesOrderDetailService.addOrderDetail(semiProductSalesOrderDetail);
            if (result) {
                return Result.OK("添加成功！");
            } else {
                return Result.error("添加失败！");
            }
        } catch (Exception e) {
            log.error("添加订单明细失败", e);
            return Result.error("添加失败：" + e.getMessage());
        }
    }

    /**
     * 编辑
     */
    @AutoLog(value = "半成品销售订单明细-编辑")
    @ApiOperation(value = "半成品销售订单明细-编辑", notes = "半成品销售订单明细-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SemiProductSalesOrderDetail semiProductSalesOrderDetail) {
        try {
            boolean result = semiProductSalesOrderDetailService.updateOrderDetail(semiProductSalesOrderDetail);
            if (result) {
                return Result.OK("编辑成功!");
            } else {
                return Result.error("编辑失败!");
            }
        } catch (Exception e) {
            log.error("编辑订单明细失败", e);
            return Result.error("编辑失败：" + e.getMessage());
        }
    }

    /**
     * 通过id删除
     */
    @AutoLog(value = "半成品销售订单明细-通过id删除")
    @ApiOperation(value = "半成品销售订单明细-通过id删除", notes = "半成品销售订单明细-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        try {
            boolean result = semiProductSalesOrderDetailService.deleteOrderDetail(id);
            if (result) {
                return Result.OK("删除成功!");
            } else {
                return Result.error("删除失败!");
            }
        } catch (Exception e) {
            log.error("删除订单明细失败", e);
            return Result.error("删除失败：" + e.getMessage());
        }
    }

    /**
     * 批量删除
     */
    @AutoLog(value = "半成品销售订单明细-批量删除")
    @ApiOperation(value = "半成品销售订单明细-批量删除", notes = "半成品销售订单明细-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        try {
            this.semiProductSalesOrderDetailService.removeByIds(java.util.Arrays.asList(ids.split(",")));
            return Result.OK("批量删除成功!");
        } catch (Exception e) {
            log.error("批量删除订单明细失败", e);
            return Result.error("批量删除失败：" + e.getMessage());
        }
    }

    /**
     * 通过id查询
     */
    @AutoLog(value = "半成品销售订单明细-通过id查询")
    @ApiOperation(value = "半成品销售订单明细-通过id查询", notes = "半成品销售订单明细-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SemiProductSalesOrderDetail> queryById(@RequestParam(name = "id", required = true) String id) {
        try {
            SemiProductSalesOrderDetail semiProductSalesOrderDetail = semiProductSalesOrderDetailService.getById(id);
            if (semiProductSalesOrderDetail == null) {
                return Result.error("未找到对应数据");
            }
            return Result.OK(semiProductSalesOrderDetail);
        } catch (Exception e) {
            log.error("查询订单明细失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据物料ID查询明细列表
     */
    @AutoLog(value = "半成品销售订单明细-根据物料ID查询")
    @ApiOperation(value = "半成品销售订单明细-根据物料ID查询", notes = "半成品销售订单明细-根据物料ID查询")
    @GetMapping(value = "/getByMaterialId")
    public Result<List<SemiProductSalesOrderDetail>> getByMaterialId(@RequestParam(name = "materialId", required = true) String materialId) {
        try {
            List<SemiProductSalesOrderDetail> list = semiProductSalesOrderDetailService.getByMaterialId(materialId);
            return Result.OK(list);
        } catch (Exception e) {
            log.error("根据物料ID查询明细失败: materialId={}", materialId, e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据物料编码查询明细列表
     */
    @AutoLog(value = "半成品销售订单明细-根据物料编码查询")
    @ApiOperation(value = "半成品销售订单明细-根据物料编码查询", notes = "半成品销售订单明细-根据物料编码查询")
    @GetMapping(value = "/getByMaterialCode")
    public Result<List<SemiProductSalesOrderDetail>> getByMaterialCode(@RequestParam(name = "materialCode", required = true) String materialCode) {
        try {
            List<SemiProductSalesOrderDetail> list = semiProductSalesOrderDetailService.getByMaterialCode(materialCode);
            return Result.OK(list);
        } catch (Exception e) {
            log.error("根据物料编码查询明细失败: materialCode={}", materialCode, e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 批量保存订单明细
     */
    @AutoLog(value = "半成品销售订单明细-批量保存")
    @ApiOperation(value = "半成品销售订单明细-批量保存", notes = "半成品销售订单明细-批量保存")
    @PostMapping(value = "/saveBatch")
    public Result<String> saveBatch(@RequestParam(name = "orderId", required = true) String orderId,
                                   @RequestBody List<SemiProductSalesOrderDetail> detailList) {
        try {
            boolean result = semiProductSalesOrderDetailService.saveOrderDetailBatch(orderId, detailList);
            if (result) {
                return Result.OK("批量保存成功！");
            } else {
                return Result.error("批量保存失败！");
            }
        } catch (Exception e) {
            log.error("批量保存订单明细失败", e);
            return Result.error("批量保存失败：" + e.getMessage());
        }
    }
}
